package pkg;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DataFeedHandler implements IRequestHandler
{
	@Override
	public void handleRequest(HttpServletRequest request,
			HttpServletResponse response, IStatelessBean stateless,
			IStatefulBean stateful) throws ServletException, IOException
	{
		String idString = request.getParameter("id");
    	System.out.println("Test page request id = " + idString);
    	if(idString == null) 
    	{
    		stateful.setCurrentPage(stateless.getRootPage());
    	}
    	else 
    	{
    		long id = Long.parseLong(idString);
    		stateful.setCurrentPage(stateless.getPageById(id));
    	}
    	
		
    	StringBuilder sb = new StringBuilder();

    	String command = request.getParameter("type");

    	if(command == null)
    	{
    		
    	}
    	else if(command.equals("categories"))
    	{
    		List<String> categories = stateless.getDataFeedCategories();
    	    //sb.append("<root>\n");
    	    
    	    //for (String s : categories)
    	    //{
	    	//	sb.append("<category>\n");
	    	//	sb.append("<name>" + s + "</name>");
	    	//	sb.append("</category>\n");
    	    //}
    	    //sb.append("</root>\n");
    	}
    	
		/*
    	
    	else if(command.equalsIgnoreCase("dataitems"))
    	{
    		List<String> dataItems = stateless.getDataFeedDataItems();
    	    sb.append("<root>\n");
    	    
    	    for (String s : dataItems)
    	    {
	    		sb.append("<datattem>\n");
	    		sb.append("<name>" + s + "</name>");
	    		sb.append("</dataitem>\n");
    	    }
    	    sb.append("</root>\n");
    	}
    	    
    	  /*  <root>
    	    while(result.next())
    	    {
    	    	
    	    		<DataItem>
    	    			<Name>result.getString("name")</Name>
    	    			
    	    				sql = "SELECT name WHERE name = '" + result.getString("name") + "' FROM CATEGORY";
    	    				result2 = statement.executeQuery(sql);
    	    			
    	    			<Category> result2.getString("name") </Category>
    	    			
    	    				sql = "SELECT name WHERE name = '" + result.getString("name") + "' FROM UNIT";
    	    				result2 = statement.executeQuery(sql);
    	    			
    	    			<Unit> result2.getString("name") </Unit>
    	    		</DataItem>
    	    	
    	    }
    	    </root>
    	}
    	else if(command.equals("Data"))
    	{
    		String dataItem = request.getParameter("DataItem");
    		String startTime = request.getParameter("StartTime");
    		String endTime = request.getParameter("EndTime");
    		
    		sql = "SELECT 'name' FROM 'DATA'";
    		
    		try 
    	    { 
    			result = statement.executeQuery(sql); 
    	    } 
    	    catch (Exception ex) 
    	    { 
    	    	
    	    }
    	    
    	    %><root><%
    	    while(result.next())
    	    {
    	    	%>
    	    		<Categories><%=result%></Categories>
    	    	<%
    	    }
    	    %></root><%
    	}
    	
        RequestDispatcher rd = request.getRequestDispatcher("test2.jsp");
        rd.forward(request, response);
*/
	}
}


